package lc.q150_200;

import lc.TreeNode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;

public class Q199 {

    public static void main(String[] args) {

    }

    public List<Integer> rightSideView(TreeNode root) {
        if (root == null) {
            return new ArrayList<>();
        }
        List<Integer> result = new ArrayList<>();
        levelIndex(Collections.singletonList(root), result);
        return result;
    }

    public void levelIndex(List<TreeNode> topLevels, List<Integer> result) {
        if (topLevels.isEmpty()) {
            return;
        }
        result.add(topLevels.get(topLevels.size() - 1).val);
        List<TreeNode> checkNext = new ArrayList<>();
        for (TreeNode topLevel : topLevels) {
            if (topLevel.left != null) {
                checkNext.add(topLevel.left);
            }
            if (topLevel.right != null) {
                checkNext.add(topLevel.right);
            }
        }
        levelIndex(checkNext, result);
    }
}
